package com.week.controller;

import com.alibaba.fastjson.JSONObject;
import com.week.entity.SysUser;
import com.week.entity.TelReq;
import com.week.feign.SysUserFeign;
import com.week.result.Result;
import com.week.util.JwtUtils;
import com.week.util.TelSmsUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@Log4j2
public class AuthController {

    private  final RedisTemplate<String,String> redisTemplate;
    private final SysUserFeign sysUserFeign;

    private final RabbitTemplate rabbitTemplate;

    public AuthController(RedisTemplate<String, String> redisTemplate, SysUserFeign sysUserFeign, RabbitTemplate rabbitTemplate) {
        this.redisTemplate = redisTemplate;
        this.sysUserFeign = sysUserFeign;
        this.rabbitTemplate = rabbitTemplate;
    }


    /**
     *
     * 发送短信
     */
    @PostMapping("/send/tel")
    public Result sendTel(@RequestBody TelReq telReq){

        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(10, 16);
        String sendSms = TelSmsUtils.sendSms(telReq.getTel(), "SMS_153991546", new HashMap<String, String>() {{
            put("code", code);
        }});
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        ops.set(telReq.getTel(),code,1, TimeUnit.MINUTES);
        log.info("手机号:{},验证码:{},阿里云调用结果{}",telReq.getTel(),code,sendSms);

        return  Result.success();
    }


    @PostMapping("/login")
    public Result login(@RequestBody SysUser sysUser){
        String tel = sysUser.getTel();
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        String s = ops.get(tel);
        if (s==null){
            return Result.error("用户名错误");
        }
        if (!s.equals(sysUser.getPassword())){
            return Result.error("用户密码错误");
        }

        Result<SysUser> sysUserResult = sysUserFeign.findOne(tel);
        SysUser data = sysUserResult.getData();

        if (data==null){
            return Result.error("手机未注册");
        }
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put(JwtUtils.USER_ID,data.getId());

        String token = JwtUtils.createToken(hashMap);

        data.setToken(token);
        ops.set(JwtUtils.TOKEN +data.getId(), JSONObject.toJSONString(data),5,TimeUnit.MINUTES);


        rabbitTemplate.convertAndSend("sys_user",data.getId());
        log.info("消息队列{},发送消息{}","sys_user",data.getId());
        return Result.success(new HashMap<String,String>(){{
            put(JwtUtils.TOKEN,token);
            put("time","5MIN");
        }});

    }


}
